package com.javacoo.swing.jxbrowser.content;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import com.javacoo.swing.jxbrowser.BaseBrowser;
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

/**
 * 获取HTML
 * <li></li>
 * 要获取表示已加载网页的HTML的字符串，请使用Browser.getHTML（）方法。只有在完全加载网页时才能调用此方法。否则，您可能会收到不完整的HTML或空字符串。以下代码演示了如何等待网页完全加载并获取其HTML
 * @author: duanyong@jccfc.com
 * @since: 2020/9/25 9:28
 */
public class GettingHtml extends BaseBrowser {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addLoadListener(new LoadAdapter() {
            @Override
            public void onFinishLoadingFrame(FinishLoadingEvent event) {
                if (event.isMainFrame()) {
                    System.out.println("HTML = " + event.getBrowser().getHTML());
                }
            }
        });
        browser.loadURL(BASE_URL);
    }
}
